<?php
require_once FOREGROUND_COMMON_ACTION_SERVICE_DIR . '/ajax.php';

class foreground_member_do_change_password_action_service extends foreground_ajax_common_action_service
{
    protected static $_instance;

    public static function get_instance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }


    protected function _current_page_process()
    {	     		     
	    //var_dump(__METHOD__); exit();
	
	    $result = array('status' => 0, 'message' => '', 'data' => array());
	
	    //var_dump($_POST); exit();
	    $origin_password = init_user_data_key($_POST, 'origin_password', 'str');
	    $password = init_user_data_key($_POST, 'password', 'str');
	    $confirm_password = init_user_data_key($_POST, 'confirm_password', 'str');
	    
	    $action_data = wwy_action::$data;
	    $session_member_info = $action_data['session_member_info'];
	    $member_id = $session_member_info['id'];	    
	    
        if(empty($origin_password)){
	        $result['status']  = -2;
	        $result['message'] = '请输入原密码';
	    }elseif(empty($password)){
	        $result['status']  = -3;
	        $result['message'] = '请输入密码';
	    }elseif(empty($confirm_password)){
	        $result['status']  = -4;
	        $result['message'] = '请输入确认密码';
	    }elseif($password == $origin_password){
	        $result['status']  = -5;
	        $result['message'] = '新密码不能和原密码一样';
	    }elseif($confirm_password != $password){
	        $result['status']  = -6;
	        $result['message'] = '新密码不一致';
	    }		    
	
	    if($result['status'] == 0){
	        $model_foreground_member = wwy_resource_container::get_model_foreground_member();
	        //var_dump($model_foreground_member); exit();
	        $member  = $model_foreground_member->find($member_id);
	        //var_dump($member); exit();
	        if(empty($member)){
	            $result['status']  = -7;
                $result['message'] = '修改失败';
	        }else{
	            $origin_password = md5($origin_password);
	            //var_dump($origin_password); exit();
	            if($origin_password != $member['password']){
	                $result['status']  = -8;
                    $result['message'] = '原密码不正确';
	            }else{
	                $password = md5($password);
	                $time = time();	                
	                $member_info = array(
	                    'id'          => $member['id'],
	                    'password'    => $password,
	                    'update_time' => $time
	                );
	                $update_member_password_result = $model_foreground_member->update_member_password($member_info);
	                //var_dump($update_member_password_result); exit();
	
	                if(!$update_member_password_result){
	                    $result['status']  = -9;
                        $result['message'] = '修改密码失败';
	                }else{
	                    $result['status']  = 1;
	                    $result['message'] = '修改密码成功，请重新登录';
	                }
	            }
	             
	        }
	    }    	    
	
	    self::set_view_status($result['status']);
	    self::set_view_message($result['message']);
    }
}